const koa = require('koa')
const cors = require('koa2-cors')
const bodyParser = require('koa-bodyparser')
const userRouter = require('./router/User')
const app = new koa()
const koaJwt = require('koa-jwt')

app.use(cors());
app.use(bodyParser({
	enableTypes:['json', 'form', 'text']
}))

const Config = {
	secretSalt:  'YIKESAITING'
}

// Custom 401 handling if you don't want to expose koa-jwt errors to users
app.use(function(ctx, next){
    return next().catch((err) => {
        if (401 == err.status) {
            ctx.status = 401;
            ctx.body = 'token is expired';
        } else {
            throw err;
        }
    });
});

app.use(koaJwt({secret: Config.secretSalt}).unless({
    path:[/^\/auth\/login/]
}))


app.use(userRouter.routes(), bodyParser(), userRouter.allowedMethods());

app.listen(4000, () => {
	console.log('server run on port 4000')
})
